package com.ruoyi.purchase.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.purchase.domain.InspectionItem;
import com.ruoyi.purchase.mapper.InspectionSchemeMapper;
import com.ruoyi.purchase.domain.InspectionScheme;
import com.ruoyi.purchase.service.IInspectionSchemeService;

/**
 * 检验方案Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-31
 */
@Service
public class InspectionSchemeServiceImpl implements IInspectionSchemeService 
{
    @Autowired
    private InspectionSchemeMapper inspectionSchemeMapper;

    /**
     * 查询检验方案
     * 
     * @param id 检验方案主键
     * @return 检验方案
     */
    @Override
    public InspectionScheme selectInspectionSchemeById(Long id)
    {
        return inspectionSchemeMapper.selectInspectionSchemeById(id);
    }

    /**
     * 查询检验方案列表
     * 
     * @param inspectionScheme 检验方案
     * @return 检验方案
     */
    @Override
    public List<InspectionScheme> selectInspectionSchemeList(InspectionScheme inspectionScheme)
    {
        return inspectionSchemeMapper.selectInspectionSchemeList(inspectionScheme);
    }

    /**
     * 新增检验方案
     * 
     * @param inspectionScheme 检验方案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInspectionScheme(InspectionScheme inspectionScheme)
    {
        int rows = inspectionSchemeMapper.insertInspectionScheme(inspectionScheme);
        insertInspectionItem(inspectionScheme);
        return rows;
    }

    /**
     * 修改检验方案
     * 
     * @param inspectionScheme 检验方案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInspectionScheme(InspectionScheme inspectionScheme)
    {
        inspectionSchemeMapper.deleteInspectionItemBySchemeId(inspectionScheme.getId());
        insertInspectionItem(inspectionScheme);
        return inspectionSchemeMapper.updateInspectionScheme(inspectionScheme);
    }

    /**
     * 批量删除检验方案
     * 
     * @param ids 需要删除的检验方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInspectionSchemeByIds(Long[] ids)
    {
        inspectionSchemeMapper.deleteInspectionItemBySchemeIds(ids);
        return inspectionSchemeMapper.deleteInspectionSchemeByIds(ids);
    }

    /**
     * 删除检验方案信息
     * 
     * @param id 检验方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInspectionSchemeById(Long id)
    {
        inspectionSchemeMapper.deleteInspectionItemBySchemeId(id);
        return inspectionSchemeMapper.deleteInspectionSchemeById(id);
    }

    /**
     * 新增检验项目信息
     * 
     * @param inspectionScheme 检验方案对象
     */
    public void insertInspectionItem(InspectionScheme inspectionScheme)
    {
        List<InspectionItem> inspectionItemList = inspectionScheme.getInspectionItemList();
        Long id = inspectionScheme.getId();
        if (StringUtils.isNotNull(inspectionItemList))
        {
            List<InspectionItem> list = new ArrayList<InspectionItem>();
            for (InspectionItem inspectionItem : inspectionItemList)
            {
                inspectionItem.setSchemeId(String.valueOf(id));
                list.add(inspectionItem);
            }
            if (list.size() > 0)
            {
                inspectionSchemeMapper.batchInspectionItem(list);
            }
        }
    }
}
